package name.sra.png;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {

	public static BufferedImage getImage(File file) {
		BufferedImage img = null;
		try {
		    img = ImageIO.read(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return img;
	}
	
	public static String correctRGB(String rgb) {
		int nbZero = 32 - rgb.length();
		String result = "";
		for (int i = 0;i < nbZero; i++) {
			result += "0";
		}
		return result + rgb;
	}
	
	public static int setLum(int rgb, String lum) {
		
		int offset = 0;
		int lumValue = Integer.parseInt(lum, 16);
		String lumBytes =Integer.toBinaryString(lumValue);
		
		if (lumBytes.charAt(0) == '1') {
			offset = Integer.MIN_VALUE;
		}
		String rgbBytes = correctRGB(Integer.toBinaryString(rgb));
		
		return Integer.parseInt(lumBytes.substring(1) + rgbBytes.substring(8), 2) + offset;
	}
}
